package com.eshang.test.thymeleaf.controller;

import com.esang.test.base.entity.StuClass;
import com.esang.test.base.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("controller")
public class ThymeleafController {

	@RequestMapping("toIndex")
	//map是用来返回数据给前端的
	public String toIndex(Map<String,Object> map){
		//为thymeleaf准备数据
		map.put("data", "这是从后端传过来的数据");
		//返回值代表着 跳转到resources/templates/index.html
		return "index";
	}

	@RequestMapping("toTestHref")
	public String toTestHref(){
		return "testHref";
	}

	@RequestMapping("toTestVariable")
	public String toTestVariable(Map<String, Object> map) {
		//01. 普通字符串
		String name = "小红";

		//02. 对象
		Student student1 = new Student();
		student1.setStuName("妮妮");
		student1.setStuNo(1);
		student1.setStuSex("女");

		Student student2 = new Student();
		student2.setStuName("敏敏");
		student2.setStuNo(2);
		student2.setStuSex("女");

		//03. List
		List<Student> studentsList = new ArrayList<>();
		studentsList.add(student1);
		studentsList.add(student2);

		//04. map
		Map<String, Object> studentsMap = new HashMap<>();
		studentsMap.put("student1", student1);
		studentsMap.put("student2", student2);

		//05. 级联对象
		StuClass stuClass = new StuClass();
		stuClass.setStuClassId(1);
		stuClass.setStuClassName("一班");
		stuClass.setStudents(studentsList);

		//返回给前端
		map.put("name", name);
		map.put("student1", student1);
		map.put("studentsList", studentsList);
		map.put("studentsMap", studentsMap);
		map.put("stuClass", stuClass);

		return "testVariable";
	}

}
